<?php

class History extends CActiveRecord
{
	/**
	 * The followings are the available columns in table '{{history}}':
	 * @var integer $id
	 * @var integer $status
	 * @var string $table
	 * @var integer $id_id
	 * @var string $column
	 * @var string $value
	 * @var string $new_value
	 * @var integer $admin_id
	 * @var string $admin_ip
	 * @var string $update_time
	 */
	
	const STATUS_ADD=1;
	const STATUS_UPDATE=2;
	const STATUS_DELETE=3;
	const STATUS_UNDELETE=4;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{history}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('status, id_id, admin_id', 'numerical', 'integerOnly'=>true),
			array('table, column, value, new_value', 'length', 'max'=>128),
			array('admin_ip', 'length', 'max'=>25),
			array('update_time', 'type', 'type'=>'datetime', 'datetimeFormat'=>Yii::app()->locale->dateFormat.' '.Yii::app()->locale->timeFormat),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, status, table, id_id, column, value, new_value, admin_id, admin_ip, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'admin'=>array(self::BELONGS_TO,'Admin','admin_id',
						'select'=>'id,username',),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('main','Id'),
			'status' => Yii::t('main','Type'),
			'table' => Yii::t('main','Table'),
			'id_id' => Yii::t('main','Id'),
			'column' => Yii::t('main','Column'),
			'value' => Yii::t('main','Value'),
			'new_value' => Yii::t('main','New Value'),
			'admin_id' => Yii::t('main','Admin'),
			'admin_ip' => Yii::t('main','Admin Ip'),
			'update_time' => Yii::t('main','Update Time'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria(array('with'=>array('admin')));

		$criteria->compare('t.id',$this->id);

		$criteria->compare('t.status',$this->status);

		$criteria->compare('t.table',$this->table,true);

		$criteria->compare('t.id_id',$this->id_id);

		$criteria->compare('t.column',$this->column,true);

		$criteria->compare('t.value',$this->value,true);

		$criteria->compare('t.new_value',$this->new_value,true);

		$criteria->compare('admin.username',$this->admin_id,true);

		$criteria->compare('t.admin_ip',$this->admin_ip,true);

		$criteria->compare('t.update_time',$this->update_time,true);

		return new CActiveDataProvider('History', array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>Setting::get('PerPage')),
			'sort'=>array(
				'defaultOrder'=>'t.update_time DESC',
			),
		));
	}
	
	public function behaviors()
    {
        return array(
            'DateTime' => array(
            	'class'=>'application.components.behaviors.DateTimeBehavior',
            ),
        );
    }
}